package Model;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by anton.kasiyanenko@stud.fh-luebeck.de on 21.10.2014.
 */
public class Map {
    private static Random random = new Random(System.currentTimeMillis());
    public int rows;
    public int cols;
    public int cellSize;

    private Object[][] grid;
    private List<Pedestrian>	pedestrians = new ArrayList<Pedestrian>();
    private Player player;

    public Map(Player player, int width, int height, int scale, int cellSize) {
        this.player = player;

        this.cols = width / cellSize;
        this.rows = height / cellSize;
        this.cellSize = cellSize * scale;

        grid = new Object[rows][cols];
        for (int i = 0; i < cols; i++) {
            for (int j = 0; j < rows; j++) {
                grid[j][i] = null;
            }
        }

        // spieler platzieren
        grid[(int)player.getY()][(int) player.getX()] = player;

        // Fussgaenger hinzufuegen
        for (int i = 0; i < 50; i++) {
//            int x = random.nextInt(cols);
//            int y = random.nextInt(rows);
            int x = random.nextInt(cols);
            int y = getRandomMinMax(5, rows-4 );

            while (grid[y][x] != null) {
//                x = random.nextInt(cols);
//                y = random.nextInt(rows);
                x = random.nextInt(cols);
                y = getRandomMinMax(5, rows-4 );
            }

            float speed = 2f;
            // etwa in 50% der faelle wollen wir in die andere richtung
            if(random.nextInt(2) == 1) {
                speed = -2f;
            }

            grid[y][x] = new Pedestrian(x, y, speed);
            pedestrians.add((Pedestrian) grid[y][x]);
        }
    }

    public List<Pedestrian> getPedestrians() {
        return pedestrians;
    }

    public Player getPlayer() {
        return player;
    }

    public Object[][] getGrid() {
        return grid;
    }

    private int getRandomMinMax(int min, int max) {
        return random.nextInt((max - min) + 1) + min;
    }
}
